// pages/login/login.js
import request from '../../api/request'
import {
  getPhoneNumber,
  setPhoneNumber,
  decrypts
} from '../../api/phoneLogin'
import {
  jump
} from '../../api/public'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    options: {},
    tokens:wx.getStorageSync('tokens') || false,
    navdata:{
      loading: false,
      color: '#fff',
      background: '#f8f8f8',
      show: true,
      animated: false
    },
    agreementIndex:false,
  },
  checkboxChange(e){

    let index = e.detail.value.length
    this.setData({
      agreementIndex:index
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.options = options;
    console.log(options)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.clearStorageSync()
  },
  goAgreement() {
    wx.navigateTo({
      url: '/pages/agreement/agreement?url='+'http://api.qihaochuxing.com/protocol/agreement/user.html',
    })
  },
  go() {
    wx.navigateTo({
      url: '/pages/agreement/agreement?url='+'http://api.qihaochuxing.com/protocol/agreement/useCar.html',
    })
  },
  loginErr(){
    if(!this.agreementIndex){
      wx.showToast({
        title:'请阅读并勾选用户协议、租车协议'
      })
      return
    }
  },
  getPhone(e) {
    if (e.detail.errMsg === "getPhoneNumber:ok") {
      wx.login({
        success: res => {
          console.log(res)
          // decrypts({
          //   data:{
          //     iv:e.detail.iv,
          //     encrypted_data:e.detail.encryptedData
          //   }
          // })
          getPhoneNumber({
            data: {
              code: res.code,
            }
          }).then(eve => {
            console.log(eve,'eeeeeeeee')
            decrypts({
              data:{
                iv:e.detail.iv,
                encrypted_data:e.detail.encryptedData
              }
            })
            .then(res=>{
              let p = JSON.stringify(res.data.phoneNumber)
              wx.setStorageSync('phone', p)
              setPhoneNumber({
                data:{
                  phone:res.data.phoneNumber
                }
              })
              wx.navigateBack({ //返回
                delta: 1
              })
            })
          })
        }
      })
    } else {
      wx.showToast({
        title: "授权失败，请重试",
        icon: "none"
      })
    }
  },
  goPhoneLogin() {
    wx.navigateTo({
      url: '/pages/phoneLogin/phoneLogin',
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})